import { defineStore } from "pinia";
import { ref, computed } from "vue";

// 初始状态
function initState() {
    return {
        // FAQ列表
        faqs: [],
        // 按分类缓存的FAQ
        faqsByCategory: {},
        // 加载状态
        loading: false,
        // 错误信息
        error: null,
        // 是否已加载数据
        isLoaded: false
    };
}

export const useFAQStore = defineStore("faqStore", () => {
    // 状态
    const state = ref(initState());

    // 计算属性
    const getAllFAQs = computed(() => state.value.faqs);
    const getFAQsByCategory = computed(() => (category) => {
        return state.value.faqsByCategory[category] || [];
    });
    const hasFAQs = computed(() => state.value.faqs.length > 0);
    const hasFAQsForCategory = computed(() => (category) => {
        return state.value.faqsByCategory[category] && state.value.faqsByCategory[category].length > 0;
    });
    const isLoading = computed(() => state.value.loading);
    const getError = computed(() => state.value.error);
    const isLoaded = computed(() => state.value.isLoaded);

    // 设置FAQ列表
    function setFAQs(faqList) {
        state.value.faqs = faqList || [];
        state.value.error = null;
        state.value.isLoaded = true;
    }

    // 设置按分类的FAQ列表
    function setFAQsByCategory(category, faqList) {
        state.value.faqsByCategory[category] = faqList || [];
        state.value.error = null;
    }

    // 设置加载状态
    function setLoading(loadingState) {
        state.value.loading = loadingState;
    }

    // 设置错误信息
    function setError(errorMessage) {
        state.value.error = errorMessage;
    }

    // 清除错误信息
    function clearError() {
        state.value.error = null;
    }

    // 清除所有数据
    function clearAll() {
        Object.assign(state.value, initState());
    }

    // 根据ID查找FAQ
    function findFAQById(id) {
        return state.value.faqs.find(faq => faq.id === id) || null;
    }

    // 添加单个FAQ到列表
    function addFAQ(faq) {
        if (faq && !state.value.faqs.find(f => f.id === faq.id)) {
            state.value.faqs.push(faq);
        }
    }

    // 更新FAQ
    function updateFAQ(updatedFAQ) {
        const index = state.value.faqs.findIndex(faq => faq.id === updatedFAQ.id);
        if (index !== -1) {
            state.value.faqs[index] = updatedFAQ;
        }

        // 同时更新分类缓存中的数据
        Object.keys(state.value.faqsByCategory).forEach(category => {
            const categoryIndex = state.value.faqsByCategory[category].findIndex(faq => faq.id === updatedFAQ.id);
            if (categoryIndex !== -1) {
                state.value.faqsByCategory[category][categoryIndex] = updatedFAQ;
            }
        });
    }

    // 删除FAQ
    function removeFAQ(id) {
        state.value.faqs = state.value.faqs.filter(faq => faq.id !== id);

        // 同时从分类缓存中删除
        Object.keys(state.value.faqsByCategory).forEach(category => {
            state.value.faqsByCategory[category] = state.value.faqsByCategory[category].filter(faq => faq.id !== id);
        });
    }

    // 重置状态
    function resetState() {
        Object.assign(state.value, initState());
    }

    return {
        state,
        getAllFAQs,
        getFAQsByCategory,
        hasFAQs,
        hasFAQsForCategory,
        isLoading,
        getError,
        isLoaded,
        setFAQs,
        setFAQsByCategory,
        setLoading,
        setError,
        clearError,
        clearAll,
        findFAQById,
        addFAQ,
        updateFAQ,
        removeFAQ,
        resetState
    };
}, {
    persist: {
        key: 'faq-store',
        storage: localStorage,
        paths: ['state.faqs', 'state.faqsByCategory', 'state.isLoaded']
    }
});